package artid.com.arttogo.view;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import artid.com.arttogo.view.pulllist.PullableScrollView;


/**
 * 
 *    
 * 项目名称：ArtworkArt   
 * 类名称：ZoomScollView   
 * 类描述：自定义ScollView下拉放大图片  
 * 创建人：耿卫斌   
 * 创建时间：2017-2-7 下午1:34:37   
 *
 */
public class ZoomScollView extends PullableScrollView {
	private Boolean isSlide=false;//是否可以滑动默认禁止滑动，这里是防止图片加载出的一瞬间手指正在放大图片产生的BUG
	public Boolean ZoomHeight=false;//设置只放大高度
	public ZoomScollView(Context context) {
		super(context);
	}

	public ZoomScollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ZoomScollView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	//    用于记录下拉位置
	private float y = 0f;
	//    zoomView原本的宽高
	private int zoomViewWidth = 0;
	private int zoomViewHeight = 0;

	//    是否正在放大
	private boolean mScaling = false;

	//    放大的view，默认为第一个子view
	private View zoomView;
	public void setZoomView(View zoomView, Boolean isSlide) {
		this.zoomView = zoomView;
		this.isSlide=isSlide;
	}

	//    滑动放大系数，系数越大，滑动时放大程度越大
	private float mScaleRatio = 0.4f;
	public void setmScaleRatio(float mScaleRatio) {
		this.mScaleRatio = mScaleRatio;
	}

	//    最大的放大倍数
	private float mScaleTimes = 2f;
	public void setmScaleTimes(int mScaleTimes) {
		this.mScaleTimes = mScaleTimes;
	}

	//    回弹时间系数，系数越小，回弹越快
	private float mReplyRatio = 0.5f;
	public void setmReplyRatio(float mReplyRatio) {
		this.mReplyRatio = mReplyRatio;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		//        不可过度滚动，否则上移后下拉会出现部分空白的情况
		setOverScrollMode(OVER_SCROLL_NEVER);
		//        获得默认第一个view
		if (getChildAt(0) != null && getChildAt(0) instanceof ViewGroup && zoomView == null) {
			ViewGroup vg = (ViewGroup) getChildAt(0);
			if (vg.getChildCount() > 0) {
				zoomView = vg.getChildAt(0);
			}
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if(isSlide){
			if (zoomViewWidth <= 0 || zoomViewHeight <=0) {
				zoomViewWidth = zoomView.getMeasuredWidth();
				zoomViewHeight = zoomView.getMeasuredHeight();
			}
			if (zoomView == null || zoomViewWidth <= 0 || zoomViewHeight <= 0) {
				return super.onTouchEvent(ev);
			}
			switch (ev.getAction()) {
			case MotionEvent.ACTION_MOVE:
				if (!mScaling) {
					if (getScrollY() == 0) {
						y = ev.getY();//滑动到顶部时，记录位置
					} else {
						break;
					}
				}
				int distance = (int) ((ev.getY() - y)*mScaleRatio);
				if (distance < 0) break;//若往下滑动
				mScaling = true;
				setZoom(distance);
				return true;
			case MotionEvent.ACTION_UP:
				mScaling = false;
				replyView();
				break;
			}
		}
		return super.onTouchEvent(ev);
	}

	/**放大view*/
	private void setZoom(float s) {
		float scaleTimes = (float) ((zoomViewWidth+s)/(zoomViewWidth*1.0));
		//        如超过最大放大倍数，直接返回
		if (scaleTimes > mScaleTimes) return;

		ViewGroup.LayoutParams layoutParams = zoomView.getLayoutParams();
		if(!ZoomHeight) {
			layoutParams.width = (int) (zoomViewWidth + s);
		}else{
			layoutParams.width = (int) (zoomViewWidth);
		}
		layoutParams.height = (int)(zoomViewHeight*((zoomViewWidth+s)/zoomViewWidth));
		//        设置控件水平居中
		((ViewGroup.MarginLayoutParams) layoutParams).setMargins(-(layoutParams.width - zoomViewWidth) / 2, 0, 0, 0);
		zoomView.setLayoutParams(layoutParams);
	}

	/**回弹*/
	private void replyView() {
		final float distance;
		if(!ZoomHeight) {
			distance= zoomView.getMeasuredWidth() - zoomViewWidth;
		}else{
			distance = zoomView.getMeasuredHeight() - zoomViewHeight;
		}

		// 设置动画
		ValueAnimator anim = ObjectAnimator.ofFloat(distance, 0.0F).setDuration((long) (distance * mReplyRatio));
		anim.addUpdateListener( new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				setZoom((Float) animation.getAnimatedValue());
			}
		});
		anim.start();
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		if (onScrollListener!=null) onScrollListener.onScroll(l,t,oldl,oldt);
	}

	private OnScrollListener onScrollListener;
	public void setOnScrollListener(OnScrollListener onScrollListener) {
		this.onScrollListener = onScrollListener;
	}

	/**滑动监听*/
	public  interface OnScrollListener{
		void onScroll(int scrollX, int scrollY, int oldScrollX, int oldScrollY);
	}


}